<?php
/**
 * 統計画面コントローラー
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Manage_Member extends Controller_Manage {
	private $domain;

	/**
	 * before
	 *
	 * @access  public
	 * @return  Response
	 */
	public function before() {
		parent::before();

		$this -> paths = explode('/', Input::uri());

		if (!empty($_SERVER["HTTPS"])) {
			$this -> domain = 'https://';
		} else {
			$this -> domain = 'http://';
		}
		$this -> domain .= $_SERVER["HTTP_HOST"];
	}

	/**
	 * index
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_index() {
		return Response::forge(View::forge('manage/member/index'), D_HTTP_UPDATE);
	}

	/**
	 * 詳細
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_detail() {
		$action = $this -> paths[3];
		$post = Input::post();

		$views = array();
		$views['member_id'] = '';
		if (!empty($post['member_id'])) {
			$views['member_id'] = $post['member_id'];

			// プッシュ通知
			if (!empty($post['push'])) {
				$messages = array();
				$messages[] = array('token' => $post['token'], 'text' => $post['push']);
				$res = $this->apns_push($messages);
				if( $res === true ) {
					$views['message'] = 'プッシュ通知を送りました。';
				} else {
					$views['error'] = 'プッシュ通知でエラー。';
					print_r($res);
				}
			}

			// ステータス変更
			if (isset($post['status'])) {
				// ポイント付与
				$query = DB::update('member_info');
				$query -> set(array("member_status" => $post['status']));
				$query -> where('member_id', $views['member_id']);
				$query -> execute();
			}

			// ポイント追加
			if (isset($post['point'])) {
				// ポイント履歴
				$values = array("member_id" => $views['member_id'], "key1" => 'manage', "point_value" => $post['point']);
				$query = DB::insert('member_point');
				$query -> set($values);
				$query -> execute();

				// ポイント付与
				$query = DB::update('member_info');
				$query -> set(array("point_has" => DB::expr("point_has+" . $post['point'])));
				$query -> where('member_id', $views['member_id']);
				$query -> execute();
			}

			// info
			$query = DB::select('*');
			$query -> from('member_info');
			$query -> where('member_id', $views['member_id']);
			$result = $query -> execute() -> as_array();
			$views['detail'] = $result[0];

			// point
			$query = DB::select(DB::expr('COUNT(*) AS count'));
			$query -> from('member_point');
			$query -> where('member_id', $views['member_id']);
			$result = $query -> execute() -> as_array();
			$views['detail']['point_count'] = $result[0]['count'];

			// リクエスト
			$query = DB::select(DB::expr('COUNT(*) AS count'));
			$query -> from('member_request');
			$query -> where('member_id', $views['member_id']);
			$result = $query -> execute() -> as_array();
			$views['detail']['request_count'] = $result[0]['count'];

		}
		return Response::forge(View::forge('manage/member/detail', $views), D_HTTP_UPDATE);
	}

	/**
	 * ポイント一覧
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_point() {
		$action = $this -> paths[3];
		$post = Input::post();

		$query = DB::select(DB::expr('create_date AS 発生日時'), DB::expr('point_id AS ポイントID'), DB::expr('point_value AS 加算'), DB::expr('key1 AS 区分1'), DB::expr('key2 AS 区分2'));
		$query -> from('member_point');
		$query -> where('member_id', $this -> paths[3]);
		$query -> order_by('point_id', 'DESC');
		$list = $query -> execute() -> as_array();

		$csv = Input::get('csv');
		if (empty($csv)) {
			return Response::forge(View::forge('manage/table', array('list' => $list)), D_HTTP_UPDATE);
		} else {
			return Response::forge(View::forge('manage/csv', array('list' => $list)), D_HTTP_UPDATE);
		}
	}

	/**
	 * リクエスト一覧
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_request() {
		$action = $this -> paths[3];
		$post = Input::post();

		$query = DB::select(DB::expr('create_date AS 発生日時'), DB::expr('request_id AS リクエストID'), DB::expr('request_status AS ステータス'), DB::expr('giftcode AS ギフトコード'), DB::expr('key1 AS 区分1'), DB::expr('key2 AS 区分2'), DB::expr('key3 AS 区分3'), DB::expr('key4 AS 区分4'), DB::expr('key5 AS 区分5'));
		$query -> from('member_request');
		$query -> where('member_id', $this -> paths[3]);
		$query -> order_by('request_id', 'DESC');
		$list = $query -> execute() -> as_array();

		$csv = Input::get('csv');
		if (empty($csv)) {
			return Response::forge(View::forge('manage/table', array('list' => $list)), D_HTTP_UPDATE);
		} else {
			return Response::forge(View::forge('manage/csv', array('list' => $list)), D_HTTP_UPDATE);
		}
	}

	/**
	 * 一括プッシュ送信
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_pushbulk() {
		$views = array();
		$post = Input::post();
		$get = Input::get();
		
		// 送信
		if( isset($post['text'])) {
			$text = $post['text'];
			if( strlen($text) < 10 || strlen($text) > 255 ) {
				$views['error'] = '文字列長が不適切です 10 - 255バイト ('.strlen($text);
			} else {
				$query = DB::Insert('push_bulk');
				$query -> set(array('push_text' => $text));
				$query -> execute();
			}
		}
		
		// 取り消す
		if( isset($get['del'])) {
			$del = $get['del'];
			$query = DB::Update('push_bulk');
			$query -> set(array('del' => 1));
			$query -> where('push_id', $del);
			$query -> execute();
		}
		
		// 復活
		if( isset($get['ok'])) {
			$ok = $get['ok'];
			$query = DB::Update('push_bulk');
			$query -> set(array('del' => 0));
			$query -> where('push_id', $ok);
			$query -> execute();
		}
		
		// 履歴
		$query = DB::select('push_id','create_date','push_text','del');
		$query -> from('push_bulk');
		$query -> order_by('push_id','DESC');
		$query -> limit(100);
		$views['list'] = $query -> execute() -> as_array();
		
		return Response::forge(View::forge('manage/member/pushbulk', $views), D_HTTP_UPDATE);
	}

}
